#ifndef __M_CONFIG_H__
#define __M_CONFIG_H__

#include <iostream>
#include <mutex>
#include <memory>
#include "Util.hpp"

/*
    单例对象管理文件配置信息
*/

namespace cloud
{
    class config
    {
#define CONFIG_CONF "./cloud.conf"
    private:
        static std::shared_ptr<config> _instance;
        static std::mutex _mutex;

    private:
        int _host_time;
        std::string _server_Ip;
        int _server_Port;
        std::string _back_dirctory;
        std::string _pack_dictory;
        std::string _download_prefix;
        std::string _pack_suffix;
        std::string _packup_dictory;

    public:
        int GetHostTime()
        {
            return _host_time;
        }

        std::string GetServerIp()
        {
            return _server_Ip;
        }

        int GetServerPort()
        {
            return _server_Port;
        }

        std::string GetBackDictory()
        {
            return _back_dirctory;
        }

        std::string GetPackDictory()
        {
            return _pack_dictory;
        }

        std::string GetDownloadPrefix()
        {
            return _download_prefix;
        }

        std::string GetPackSuffix()
        {
            return _pack_suffix;
        }

        std::string GetBackupFile()
        {
            return _packup_dictory;
        }

        static std::shared_ptr<config> GetInstance()
        {
            if(_instance.get() == nullptr)//防止频繁加锁,提高效率s
            {
                _mutex.lock();//加锁,保证线程安全
                if(_instance.get() == nullptr)
                {
                    _instance = std::shared_ptr<config>(new config());
                }
            }
            return _instance;
        }

    private:
        config()
        {
            ReadConfigConf();
        }

        void ReadConfigConf()
        {
            Json::Value root;
            FileUtil file(CONFIG_CONF); // 打开管理配置文件
            std::string str;
            file.GetContent(&str);                  // 读取配置文件
            JsonUtil::UnSerialize(str, &root);      // 配置文件是json格式,这里要解析格式
            _host_time = root["host_time"].asInt(); // 依次赋值
            _server_Ip = root["server_Ip"].asString();
            _server_Port = root["server_port"].asInt();
            _back_dirctory = root["back_dictory"].asString();
            _pack_dictory = root["pack_dictory"].asString();
            _download_prefix = root["download_prefix"].asString();
            _pack_suffix = root["pack_suffix"].asString();
            _packup_dictory = root["packup_dictory"].asString();
        }
    };
    std::shared_ptr<config> config::_instance = std::shared_ptr<config>(nullptr);
    std::mutex config::_mutex;
}

#endif